/*
 * g2p_syllable_extract.cpp
 *
 *  Created on: Mar 2, 2013
 *      Author: think
 */
#include "g2p_syllable_extract.h"

int sx_extract(char * text, char * copy, int pos, int len) {
	printf("POS = %d\n %s", pos, text + pos);
	char max_len_text[512] = "";
	int cut_min = 0;
	int cut_len = len;
	char tmp[265] = "";
	strncpy(tmp, text, pos);
	tmp[pos] = '\0';
	int r = sylmap_map_check(tmp);
	if (r != -1) {
		cut_len = pos;
	}
	for (int j = pos; j >= 0; j = j - 3) {
		for (int i = pos; i < len; i = i + 3) {

			strncpy(tmp, text + (pos - j), i - (pos - j) + 3);
			printf("\nSa %d", i - (pos - j) + 3);
			tmp[i - (pos - j) + 3] = '\0';
			printf("\nTMP=%s %d", tmp, strlen(tmp));
			printf("\nNext : %s", text + i + 3);
			int is_bv = tone_isBackVowel(text, i + 3, len);
			printf("\nvb = %d", is_bv);
			if (tone_isBackVowel(text, i + 3, len) == -1) {
				int r = sylmap_map_check(tmp);
				if (r != -1) {
					cut_min = (pos - j);
					cut_len = strlen(tmp);
					printf("\n(OK) MIN = %d LEN=%d", cut_min, cut_len);
				}
			} else {
				printf("\ncan't map.");
			}

			printf("\n%s", tmp);
		}
	}

	printf("max =%d , len = %d", cut_min, cut_len);

	int c = 0;
	for (int i = 0; i < len; i++) {
		if (i == cut_min || i == cut_min + cut_len) {
			copy[c++] = '|';
		}
		copy[c++] = text[i];
	}
	if (cut_min == 0 && cut_len == len)
		return -1;
	else
		return 1;
}

